package com.resume.aipeople.entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import javax.persistence.*;
import java.time.LocalDateTime;

/**
 * 语音缓存实体类
 * 用于存储TTS转换后的语音数据，避免重复调用TTS服务
 */
@Entity
@Table(name = "voice_cache", 
       indexes = {
           @Index(name = "idx_text_hash", columnList = "text_hash"),
           @Index(name = "idx_character_name", columnList = "character_name"),
           @Index(name = "idx_created_at", columnList = "created_at")
       })
@Data
@NoArgsConstructor
@AllArgsConstructor
public class VoiceCache {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /**
     * 文本内容的哈希值，用于快速查找
     */
    @Column(name = "text_hash", nullable = false, unique = true)
    private String textHash;
    
    /**
     * 原始文本内容
     */
    @Column(name = "original_text", columnDefinition = "TEXT", nullable = false)
    private String originalText;
    
    /**
     * 角色名称
     */
    @Column(name = "character_name", nullable = false)
    private String characterName;
    
    /**
     * 语音数据（二进制）
     */
    @Lob
    @Column(name = "audio_data", nullable = false)
    private byte[] audioData;
    
    /**
     * 音频格式（如 mp3, wav）
     */
    @Column(name = "audio_format", nullable = false)
    private String audioFormat;
    
    /**
     * 音频文件大小（字节）
     */
    @Column(name = "audio_size")
    private Long audioSize;
    
    /**
     * 音频时长（秒）
     */
    @Column(name = "duration")
    private Double duration;
    
    /**
     * 使用的TTS服务提供商
     */
    @Column(name = "tts_provider")
    private String ttsProvider;
    
    /**
     * 创建时间
     */
    @Column(name = "created_at")
    private LocalDateTime createdAt;
    
    /**
     * 最后访问时间
     */
    @Column(name = "last_accessed_at")
    private LocalDateTime lastAccessedAt;
    
    /**
     * 访问次数
     */
    @Column(name = "access_count")
    private Long accessCount = 0L;
    
    @PrePersist
    public void prePersist() {
        this.createdAt = LocalDateTime.now();
        this.lastAccessedAt = LocalDateTime.now();
        if (this.accessCount == null) {
            this.accessCount = 0L;
        }
    }
    
    @PreUpdate
    public void preUpdate() {
        this.lastAccessedAt = LocalDateTime.now();
    }
    
    /**
     * 增加访问次数
     */
    public void incrementAccessCount() {
        this.accessCount++;
        this.lastAccessedAt = LocalDateTime.now();
    }
}
